package controller;

import implementor.PriorityImp;
import implementor.SalesPersonImp;
import implementor.CustomerInfoImp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.table.DefaultTableModel;

import daoInterfaces.PriorityDao;
import daoInterfaces.SalesPersonDao;
import daoInterfaces.CustomerInfoDao;
import dto.Priority;
import dto.SalesPerson;

import model.Lead;
import model.Model;
import utility.ViewUtilites;
import view.leeds.LeadHomeView;

public class LeadHomeController implements ActionListener
{
	LeadHomeView view;
	Lead model;
	
	
	public LeadHomeController() 
	{
		
	}


	public LeadHomeController(LeadHomeView view, Lead model) {
		super();
		this.view = view;
		this.model = model;
		view.actionListener(this);
	}


	public LeadHomeView getView() {
		return view;
	}


	public void setView(LeadHomeView view) {
		this.view = view;
	}


	public Lead getModel() {
		return model;
	}


	public void setModel(Lead model) {
		this.model = model;
	}
	
	
	
	public void setComboAttendedBy()
	{
		SalesPersonDao atten=new SalesPersonImp();
		SalesPerson[] attended=atten.retrieveAll();
		String[] items=new String[attended.length+1];
		items[0]="--Select SalesPerson Name";
		for(int i=1;i<=attended.length;i++)
		{
			items[i]=attended[i-1].getSalesPersonName();
		}
		ViewUtilites.set_comboBox(view.getComboAttended(), items);
		ViewUtilites.setAutoSuggestionsComboBox(view.getComboAttended());
	}
	
	public void setComboPriority()
	{
		PriorityDao att=new PriorityImp();
		Priority[] a=att.retrieveAll();
		String[] items=new String[a.length+1];
		items[0]="--Select Priority Name";
		for(int i=1;i<=a.length;i++)
		{
			items[i]=a[i-1].getPriorityName();
		}
		ViewUtilites.set_comboBox(view.getComboPriority(), items);
		ViewUtilites.setAutoSuggestionsComboBox(view.getComboPriority());
	}
	
	
	
	public void reactOnApplyFilter()
	{
		String attendedCombo,priorityCombo,Todate,Fromdate;
		attendedCombo=view.getComboAttended().getSelectedItem().toString();
		priorityCombo=view.getComboPriority().getSelectedItem().toString();
		
		Todate=view.getLblTo().getText();
		ViewUtilites.makeDateComponent(view.getLblTo());
		
		Fromdate=view.getLblFrom().getText();
		ViewUtilites.makeDateComponent(view.getLblFrom());
		
		java.sql.Date fdate=ViewUtilites.convert(Fromdate);
		java.sql.Date tdate=ViewUtilites.convert(Todate);
		
		
		Lead[] lead=Model.getLeadsByFilter(fdate, tdate, attendedCombo, priorityCombo);
		
		DefaultTableModel dm=(DefaultTableModel) view.getTable().getModel();
		for(int i=0;i<lead.length;i++)
		{
			dm.addRow(new Object[]{lead[i].getName(),lead[i].getPhone(),lead[i].getProductName(),lead[i].getBudget()});
		}
	
		
	}


	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		String k=arg0.getActionCommand();
		if(k.contains("APPLY FILTER"))
		{
			reactOnApplyFilter();
		}
		if(k.contains("SEND MAIL"))
		{
			
		}
		if(k.contains("SEND MESSAGE"))
		{
			
		}
		
	}
	
	

}
